package org.loon.game.tloh.edit;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;

public class FileUtil {

	private static final long serialVersionUID = -1982090153295778606L;

	final static public int EOF = -1;

	final static private String LS = System.getProperty("line.separator", "\n");

	final static private String FS = System.getProperty("file.separator", "\\");

	public static void write(File file, byte[] bytes) throws IOException {
		write(file, new ByteArrayInputStream(bytes), false);
	}

	public static void write(File file, byte[] bytes, boolean append)
			throws IOException {
		write(file, new ByteArrayInputStream(bytes), append);
	}

	public static void write(File file, InputStream input) throws IOException {
		write(file, input, false);
	}

	public static void write(File file, InputStream input, boolean append)
			throws IOException {
		mkdirs(file);
		BufferedOutputStream output = null;
		try {
			int contentLength = input.available();
			output = new BufferedOutputStream(
					new FileOutputStream(file, append));
			while (contentLength-- > 0) {
				output.write(input.read());
			}
		} finally {
			close(input, file);
			close(output, file);
		}
	}

	public static void write(File file, char[] chars) throws IOException {
		write(file, new CharArrayReader(chars), false);
	}

	public static void write(File file, char[] chars, boolean append)
			throws IOException {
		write(file, new CharArrayReader(chars), append);
	}

	public static void write(File file, String string) throws IOException {
		write(file, new CharArrayReader(string.toCharArray()), false);
	}

	public static void write(File file, String string, boolean append)
			throws IOException {
		write(file, new CharArrayReader(string.toCharArray()), append);
	}

	public static void write(File file, Reader reader) throws IOException {
		write(file, reader, false);
	}

	public static void write(File file, Reader reader, boolean append)
			throws IOException {
		mkdirs(file);
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(file, append));
			int i = -1;
			while ((i = reader.read()) != -1) {
				writer.write(i);
			}
		} finally {
			close(reader, file);
			close(writer, file);
		}
	}

	public static void write(File file, List records) throws IOException {
		write(file, records, false);
	}

	public static void write(File file, List records, boolean append)
			throws IOException {
		mkdirs(file);
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(file, append));
			for (Iterator it = records.iterator(); it.hasNext();) {
				writer.write((String) it.next());
				writer.write(LS);
			}
		} finally {
			close(writer, file);
		}
	}

	public static boolean exists(String path) {
		return new File(path).exists();
	}

	public static boolean makedirs(String path) {
		File dir = new File(path);
		if (!dir.exists()) {
			return dir.mkdirs();
		} else {
			return false;
		}
	}

	public static void deleteDir(String path) throws Exception {
		File xmlfile = new File(path);
		String[] listFile = xmlfile.list();
		if (listFile != null) {
			for (int i = 0; i < listFile.length; i++) {
				File tempfile = new File(path + FS + listFile[i]);

				if (tempfile.isDirectory()) {

					deleteDir(tempfile.getPath());
					tempfile.delete();

				}
			}
		}

	}

	public static String getCanonicalPath(File file) {
		try {
			return file.getCanonicalPath();
		} catch (IOException e) {
			return null;
		}
	}

	// helper

	private static void mkdirs(File file) throws IOException {
		checkFile(file);
		File parentFile = file.getParentFile();
		if (parentFile != null) {
			if (!parentFile.exists() && !parentFile.mkdirs()) {
				throw new IOException("Creating directories "
						+ parentFile.getPath() + " failed.");
			}
		}
	}

	private static void checkFile(File file) throws IOException {
		if (file.exists() && !file.isFile()) {
			throw new IOException("File " + file.getPath()
					+ " is actually not a file.");
		}
	}

	private static void close(InputStream input, File file) {
		if (input != null) {
			try {
				input.close();
			} catch (IOException e) {
				closingFailed(file, e);
			}
		}
	}

	private static void close(OutputStream output, File file) {
		if (output != null) {
			try {
				output.close();
			} catch (IOException e) {
				closingFailed(file, e);
			}
		}
	}

	private static void close(Reader reader, File file) {
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				closingFailed(file, e);
			}
		}
	}

	private static void close(Writer writer, File file) {
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				closingFailed(file, e);
			}
		}
	}

	private static void closingFailed(File file, IOException e) {
		String message = "Closing file " + file.getPath() + " failed.";
		throw new RuntimeException(message + ":" + e.getMessage());
	}

}
